package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ServiceCitysEntity;
import com.sgb.kjwl.model.entity.enterprise.DealedCasesEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class DealedCasesAdapter extends BaseRecycleAdapter<DealedCasesEntity> {

    Context context;

    public DealedCasesAdapter(Context context, List<DealedCasesEntity> datas) {
        super(context, R.layout.item_dealed_cases, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, DealedCasesEntity entity) {
        holder.setBinding(BR.viewModel, entity);

        TextView serviceCitys = holder.getItem().findViewById(R.id.tv_service_city);

        String cities = entity.getProvincesCode();
        StringBuilder stringBuilder = new StringBuilder();

        Gson gson = new Gson();
        ServiceCitysEntity serviceCitysEntity =  gson.fromJson(cities, ServiceCitysEntity.class);
           stringBuilder.append(serviceCitysEntity.getProvinceName()).append("-").append(serviceCitysEntity.getCityName())
                   .append("-").append(serviceCitysEntity.getAreaName());
        String provinceShow = stringBuilder.toString().trim();
        serviceCitys.setText(provinceShow);

    }

    /**
     * @param json
     * @param clazz
     * @return
     */
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {}.getType();
        ArrayList<T> arrayList = new ArrayList<>();
        try {
            ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

            for (JsonObject jsonObject : jsonObjects) {
                arrayList.add(new Gson().fromJson(jsonObject, clazz));
            }
        } catch (Exception e) {

        }

        return arrayList;
    }
    
}
